//
//  BluetoothInfoListVM.m
//  Cooper_Table_Example
//
//  Created by 计海峰 on 2021/8/19.
//  Copyright © 2021 jhfappkf@163.com. All rights reserved.
//

#import "BluetoothInfoListVM.h"
#import "BluetoothInfoModel.h"

@interface BluetoothInfoListVM ()<TableConfigProtocol>

@property (nonatomic, strong) NSMutableDictionary<NSString *,CBPeripheral *> *devices;

@end

@implementation BluetoothInfoListVM

- (void)initDown {
    [[BluetoothInfoModel shared] scanDevice];
    @weakify(self);
    [BluetoothInfoModel shared].deviceBlock = ^(NSMutableDictionary<NSString *,CBPeripheral *> *obj) {
        @strongify(self);
        self.devices = obj;
        [self forRows];
    };
}


- (void)forRows {
    NSMutableArray *rows = [NSMutableArray array];
    [self.devices enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, CBPeripheral * _Nonnull obj, BOOL * _Nonnull stop) {
        Row *row = [self genRowClassStr:@"BluetoothInfoListCell" model:obj];
        row.method = @"connect";
        [rows addObject:row];
    }];
    [self.rows removeAllObjects];
    [self.rows addObjectsFromArray:rows];
}

- (void)connect:(BindModel *)bind {
    [[BluetoothInfoModel shared] connectDeviceWithPeripheral:bind.model];
}


- (void)configController:(UIViewController *)vc; {
    vc.navigationItem.rightBarButtonItems = [JUtil barItemWithTextArray:@[@"开启", @"停止"] target:self action:@selector(barTap:)];
}

- (void)barTap:(UIButton *)sender {
    NSString *title = sender.titleLabel.text;
    if ([@"开启" isEqualToString:title]) {
        [[BluetoothInfoModel shared] scanDevice];
    }
    if ([@"停止" isEqualToString:title]) {
        [[BluetoothInfoModel shared] stopScanPeripheral];
    }
}

@end
